I've been at this for hours and can't figure it out. When I debug, it gives the above error. I'm new to this so go gentle if it's something obvious that I"m missing...
here's the code which introduced the crash from the first activity, it's still labeled 'Main':
//-- Menu Press --
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//-- Handle item selection
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
Intent menu = new Intent(this, Menu.class);
menu.putExtra("plWin", plWin);
menu.putExtra("plLoss", plLoss);
menu.putExtra("plDraw", plDraw);
startActivity(menu);
return true;
case R.id.menu_reset:
if (opCounter > plCounter) {
plLoss++;
}else if (opCounter < plCounter) {
plWin++;
}else {
plDraw++;
}
opCounter = 0;
plCounter = 0;
return true;
default:
return false;
}
}
and the second activity, currently named 'Menu':
public class Menu extends Activity {
Intent menu = getIntent();
int wins = menu.getIntExtra("plWin", 0);
int losses = menu.getIntExtra("plLoss", 0);
int draws = menu.getIntExtra("plDraw", 0);
private TextView winNum = null;
private TextView lossNum = null;
private TextView drawNum = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//-- Win Counter --
LinearLayout winView = (LinearLayout) findViewById(R.id.plLeft);
winNum = (TextView) winView.findViewById(R.id.winNum);
winNum.setText("" + wins);
//-- loss Counter --
LinearLayout lossView = (LinearLayout) findViewById(R.id.plMid);
lossNum = (TextView) lossView.findViewById(R.id.lossNum);
lossNum.setText("" + losses);
//-- Draw Counter --
LinearLayout drawView = (LinearLayout) findViewById(R.id.plRight);
drawNum = (TextView) drawView.findViewById(R.id.drawNum);
drawNum.setText("" + draws);
}
public void onBackPressed() {
finish();
super.onBackPressed();
}
}
and here's my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sesto.life.counter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.sesto.life.counter.Menu"
android:label="#string/title_activity_menu" >
</activity>
</application>
I'm looking for ideas. Thank you all in advance
I only see one possible mistake is at Intent menu = new Intent(this, Menu.class);
. Please check Menu.class is your Menu class but not Android Menu View (located at android.view.Menu). The logcat told that it can not find android.view.Menu in manifest. Hover your mouse pointer above Menu.class to check which class you imported for this.
Change
<activity
android:name="com.sesto.life.counter.Menu"
android:label="#string/title_activity_menu" >
</activity>
to
<activity
android:name=".Menu"
android:label="#string/title_activity_menu" >
</activity>
and see if that helps
I got this error message when I forgot to add The Activity in Manifest file.
Related
When I tried to give different titles in AndroidManifest.xml for each activity, app shows first title for all children activities instead off given titles. I'm new in coding so sorry for wrong explanation of my problem. The code is as following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.miwok">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NumbersActivity"
android:label="#string/category_numbers" />
<activity
android:name=".FamilyActivity"
android:label="#string/category_family" />
<activity
android:name=".ColorsActivity"
android:label="#string/category_colors" />
<activity
android:name=".PhrasesActivity"
android:label="#string/category_phrases" />
</application>
</manifest>
How can I solve it?
Update: to make it more clear I'm adding my MainActivity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
TextView numbers = (TextView) findViewById(R.id.numbers);
TextView colors = (TextView) findViewById(R.id.colors);
TextView family = (TextView) findViewById(R.id.family);
TextView phrases = (TextView) findViewById(R.id.phrases);
assert numbers != null;
numbers.setOnClickListener(this);
assert colors != null;
colors.setOnClickListener(this);
assert family != null;
family.setOnClickListener(this);
assert phrases != null;
phrases.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.numbers:
Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(numberIntent);
break;
case R.id.colors:
Intent colorsIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(colorsIntent);
break;
case R.id.family:
Intent familyIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(familyIntent);
break;
case R.id.phrases:
Intent phrasesIntent = new Intent(MainActivity.this,NumbersActivity.class);
startActivity(phrasesIntent);
break;
}
}
}
Second update:
When I copied Intents for each activity, I forgot to change activity names, so problem was ot in title but in opened activity.
You can name it in code:
by
actionbar.setTitle("title here");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!MyApplication.getInstance().isTablet())
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_settings);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(getString(R.string.settings));
....
}
So when I copied MainActivity.java from course example, all titles start working normally. The difference is that I used 'switch (view.getId())' instead of creating new onClick for each activity. I still didn't understand why it doesn't work, but hope I will learn it with more experience.
Summary:
Using 'switch' statement for activities can cause problems (for ones without knowledge,like me) in children activities titles.
I have created a simple app.
one screen of my app has following code and I use a Button (id "sendManual") to change the app view to another one.
please look at the end of this code, you can seen the button id "sendManual".
I have setup it to change screen view to App2Activity.java
(I have put the Code of App2Activity java below this code, then you can check it also)
I installed this app on my android phone, But when I click Button(id"sendManual") app suddenly Stopped and says
Unfortunately,XXXXX app has stopped
public class showPermission implements View.OnClickListener {
Dialog dialog;
Activity activity;
public void showDialog(final Activity activity){
dialog = new Dialog(activity);
dialog.setCancelable(false);
this.activity=activity;
dialog.setTitle("XXXXX");
dialog.setContentView(R.layout.permission);
Button Sendcancel = (Button) dialog.findViewById(R.id.sendCancel);
Button SendNow = (Button) dialog.findViewById(R.id.sendNow);
Button sendManual = (Button) dialog.findViewById(R.id.sendManual);
sendManual.setOnClickListener(this);
SendNow.setOnClickListener(this);
Sendcancel.setOnClickListener(this);
dialog.show();
}
#Override
public void onClick(View view) {
int id=view.getId();
if(id==R.id.sendCancel){
dialog.dismiss();
}
else if(id==R.id.sendNow){
sendSMS();
}
else if(id==R.id.sendManual){
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
}
}
Here is the code of App2Activity.java
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendSMS2();
}
});
}
............ etc
Here is manifest file.
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
You are not properly set context in Intent.Use this
Intent intent = new Intent(getApplicationContext(), App2Activity.class);
startActivity(intent);
instead of
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
This all is happening because you didn't declare App2Activity in Manifest.xml. That is compulsory to declare all the activities in manifest file.
Replace your manifest.xml with this one:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".App2Activity"/>
</application>
I have a listview in the SubActivity of my app.. but when I go from the subActivity to the main Activity and i try to return to the subActivity, the item of the listView disappear.. how to fix this problem?
also if I close my app and i restart it, my listview is empty... why?
my MainActivity:
final ArrayList<String> listCron = new ArrayList<String>();
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.history:
Intent history = new Intent(getApplicationContext(), HistoryClass.class);
history.putStringArrayListExtra("history", listCron);
startActivity(history);
break;
default:
return true;
}
return super.onOptionsItemSelected(item);
}
my SubActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history_main);
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
ArrayList<String> cronologia = i.getStringArrayListExtra("history");
ListView lv = (ListView) findViewById(R.id.viewCron);
if (cronologia.isEmpty()) {
cronologia.add("No operations");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_layout, cronologia);
lv.setAdapter(adapter);
}
}
my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HistoryClass"
android:label="#string/history">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
</application>
I have 2 projects, and I want to embed a project in other. I have created 2 projects and have made a project as library file and inserted in other but I am still unable to get it working. I have taken a simple activity and want to display a toast message, using Intent I have given the address of the next project library. This is the code of the main (first) project.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.sec_pro";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "First Activity", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, com.example.sec_pro.MainActivity.class);
EditText editText = (EditText) findViewById(R.id.editText1);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});
}
#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"?>
<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.integrate.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.sec_pro.MainActivity" >
</activity>
</application>
Now this "sec_pro" is the library file of my next project which I have inserted in the first project.
Please help.
Thanks in advance.
I think you have missed to declare your library activity in Manifest.
try this code to declare in to manifest under application tag:
<activity android:name="PackageName.YourLibraryActivity" />
Please check onClick method again, I think you missed a statement:
startActivity(intent);
I have a button that opens a new activity in Android, but it does nothing.
Java for first activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lists);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
ListView listsList = (ListView) findViewById(R.id.lists);
Button newList = (Button) findViewById(R.id.newlistbutton);
newList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), NewWishList.class);
startActivity(myIntent);
}
});
}
}
Java for second activity:
public class NewWishList extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlist);
Button back = (Button) findViewById(R.id.backbutton);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), ListOfLists.class);
startActivity(intent);
}
});
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation/keyboard change
super.onConfigurationChanged(newConfig);
RadioGroup option = (RadioGroup) findViewById(R.id.radioGroup1);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wish.list"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-feature android:name="android.hardware.screen.portrait"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar">
<activity
android:name="com.wish.list.FacebookSignIn"
android:label="#string/app_name"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListOfLists"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
></activity>
<activity
android:name=".NewWishList"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
></activity>
</application>
</manifest>
No errors in Logcat or Error Log. It's very weird. The reason I have the onConfigurationChange is because I have it set to Force Portrait Orientation on. The activities are defined in the Manifest.
First, try using
MyActivity.this
for the context.
Instead of:
v.getcontext()
try
getApplicationContext()
What's the IDE you're using? Probably Android Studio?
Do this.
Exit studio.
delete .idea\workspace.xml file
Relaunch and try again
Worked for me on Android Studio 1.0!