Helloworld Doesn't run on Emulator - java

Okay, this is really ticking me off. I ran it on both my emulator and on an android device. The code does NOT display "Helloworld, Android -mykong.com". I start the app, find it on the emulator, click on it, and it goes to interface of the app. However, it is just a blank screen! This error is not an issue of me finding the app, its not with the emulator or the android phone, the issue has to lie either in the code or in the way the code is structured/built. I know the the helloworld code is 100% correct because its from a reputable tutorial site. These are my three pairs of code. Please help, I am in desperate need!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test123"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test123.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.test123;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void activity()
{
Intent helloWorld = new Intent(getApplicationContext(), HelloWorldjavaactivity.class );
startActivity( helloWorld );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
HelloWorldjavaactivity.java
package com.example.test123;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloWorldjavaactivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView text = new TextView(this);
text.setText("Hello World, Android - mkyong.com");
setContentView(text);
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="35dp"
android:layout_marginTop="57dp"
android:text=""
/>
</RelativeLayout>

You're not starting the HelloWorldjavaactivity.java activity. You need something like the following in your MainActivity:
Intent helloWorld = new Intent(getApplicationContext(), HelloWorldjavaactivity.class );
startActivity( helloWorld );
Of course, you need that code to be triggered by some even, a button press for example.
It would be easier to just eliminate the second activity (since your MainActivity isn't doing anything anyway) and put the code in your MainActivity instead. You'll also need to edit the layout for your MainActivity so that it includes the appropriate views.

Related

Android Studio - ListView Error (Unfortunately Stopped Working)

I am new to android development and learning listview from WSCube. I have seen several other youtube videos also but cannot find the solution to it. Please check and advise.
I have attached all the codes than i have worked on my system through watching from the videos of WsCube youtube channel.
enter image description here
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.ListView"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
</application>
</manifest>
Activity_Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Main Activity
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
ArrayList<String> arrNames = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView.findViewById(R.id.listView);
arrNames.add("Daniel01");
arrNames.add("Daniel02");
arrNames.add("Daniel03");
arrNames.add("Daniel04");
arrNames.add("Daniel05");
arrNames.add("Daniel06");
arrNames.add("Daniel07");
arrNames.add("Daniel08");
arrNames.add("Daniel09");
arrNames.add("Daniel10");
arrNames.add("Daniel11");
arrNames.add("Daniel12");
arrNames.add("Daniel13");
arrNames.add("Daniel14");
arrNames.add("Daniel15");
arrNames.add("Daniel16");
arrNames.add("Daniel17");
arrNames.add("Daniel18");
arrNames.add("Daniel19");
arrNames.add("Daniel20");
ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, arrNames);
listView.setAdapter(adapter);
}
}
You made a mistake here.
listView.findViewById(R.id.listView);
That is wrong. Because you use a dot . between listView and findViewById(R.id.listView);.
You need to initialize the listView variable. You can do that with equal to = See the example I gave here.
Solution
listView = findViewById(R.id.listView);

apk giving error " unfortunately Helloapp1 stopped "

Whenever i try to make any app even simple hello world app every time i get this type of error, even in android virtual devices and android phone also, so i want to know that what are mistakes that i made every time in made, and can any one please make this code correct. So that i can run this app on my mobile phone.
this is my MainActivity_hello.java file
package com.example.helloapp1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity_hello extends Activity {
TextView tb;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_hello);
addListenerOnButton();
tb= (TextView) findViewById(R.id.Tbox);
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
tb.setText( String.format( " Hi ") );
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_hello, menu);
return true;
}
}
activity_main_activity_hello.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity_hello" >
<TextView
android:id="#+id/Tbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Tbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Hit Me" />
</RelativeLayout>
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloapp1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.helloapp1.MainActivity_hello"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
There could be many reasons, You have to be sure that:
The R. import is not R.android, which references to the Android System Resources. If it is, Your Button b is referred to the system framework button. This System Framework Button is identified with "button1".
Well, in Your case, I think You have the wright import, because You have no problem by using the id for the textView.
You gave Your RelativeLayout the same Id like the button:
android:id="#+id/button1"
The best way would be, give both another, different id.
I don´t know why You are using String.format(), just use tb.setText("Hi");
You are declaring the same id button1 twice, once in the layout and once for the button.
Change it to something else for your layout, like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/holder1"
...
>
Since it seems like you are getting started with Android development I would recommend switching over to using Android Studio.

Android Phone Call Intent

I am trying to make a call with the help of button, compiling time shows no errors but the application stops working as soon as i start the app, both on emulator and my android device. i have also set the permissions in the manifest file. but still :\
please help
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/makeCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/make_call" />
</LinearLayout>
MainActivity.java
package com.example.phonecalldemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button) findViewById(R.id.makeCall);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
makeCall();
}
});
}
protected void makeCall() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:8866158630"));
startActivity(phoneIntent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phonecalldemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission android:name="android.permission.CALL_PHONE"></permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.phonecalldemo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You can't instantiate abstract classes. Remove the 'abstract' from "public abstract class MainActivity", which will give you a different error telling you to implement the onClick method. Once you add that method that your app will launch.

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}

I’m following the android tutorial First App in the link below:
https://developer.android.com/training/basics/firstapp/starting-activity.html
The app can be launched in the emulator but the problem is when I click on the button “send, the application crashed with the message “unfortunately, my fist app has stopped”.
I think I did follow the instructions in the tutorial and can’t figure out where is the problem from.
Logcat send this message:
02-12 16:19:36.031: E/AndroidRuntime(786): java.lang.RuntimeException:
Unable to instantiate activity
ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}:
java.lang.NullPointerException
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
MAinActivity.java
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
// Do something in response to button
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
DisplayMessageActivity.java
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyFirstApp Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>
Thank you for your help
I believe the problem is
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
before onCreate(). Intent needs a Context which isn't available until the Activity has been created. Remove those lines since you already have it in onCreate().
Also, when it crashes, post your full logcat because it will tell us, usually, exactly where and what the problem is.

Cannot pass a search variable in Android search widget

I cannot pass my search query which is sent from my MainActivity.java to my SearchResultsActivity.java, with a search widget as described on the official guide.
However, I don't want my search widget to be set on the actionbar.
I got no error. My only wish is to get my query displayed thanks to "Log.v".
But the official guide seems only to explain how to call the another activity when we got a search widget inside the actionbar, which isn't our case here.
How to retrieve the variable query in my MainActivity.java ?
MainActivity.java:
package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
SearchResultsActivity.java
package com.example.fidbacks_search;
import android.os.Bundle;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class SearchResultsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
Log.v("QUERY", "this isn t even being displayed when the search button is pressed");
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.v("QUERY", query.toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_results, menu);
return true;
}
}
Searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="test"
android:hint="test2" >
</searchable>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fidbacks_search"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.fidbacks_search.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.fidbacks_search.SearchResultsActivity"
android:label="#string/title_activity_search_results"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
</manifest>
layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<SearchView
android:id="#+id/searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="144dp"
android:iconifiedByDefault="false" >
</SearchView>
</RelativeLayout>
I'm not sure exactly how you want it done, but I've used SearchView recently and to get what you want, I would simply do the following.
In your activity, add to onCreate something like:
searchView = (SearchView) findViewById(R.id.searchfield);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView.setQueryHint("Enter query here");
searchView.setOnQueryTextListener(this);
Then have your current activity implement SearchView.OnQueryTextListener's two methods; the one you are looking for is onQueryTextSubmit. Here, use the query that's passed to it and call your SearchResultsActivity.
Intent intent = new Intent(this, SearchResultsActivity.class);
intent.putExtra("query",query);
startActivity(intent);
Then have your SearchResultsActivity get the query in `onCreate':
query = getIntent().getStringExtra("query");
Then, use the query and do whatever you want with it.

Categories

Resources