0
Well, I have the MainActivity
which has two buttons, I’m trying to do something simple: When you click one, go to a Activity
, when you click on another, go to another Activity
.
This is the mainactivity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeActivity(View view){
Intent it = new Intent(MainActivity.this, HomeSpanishActivity.class);
startActivity(it);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Mainactivity.xml
<Button
android:id="@+id/btnOptionIngles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingles"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/txtWelcome"
android:layout_alignStart="@+id/txtWelcome"
android:onClick="changeActivity"/>
<Button
android:id="@+id/btnOptionEspanhol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Espanhol"
android:layout_alignTop="@+id/btnOptionIngles"
android:layout_alignRight="@+id/txtWelcome"
android:layout_alignEnd="@+id/txtWelcome"
android:layout_alignBottom="@+id/btnOptionIngles"
android:onClick="changeActivity"/>
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.edsonmarcelo.tapfun" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
</activity>
<activity
android:name=".HomeEnglishAcitivity"
android:label="@string/title_activity_home_english_acitivity" >
</activity>
<activity
android:name=".HomeSpanishActivity"
android:label="@string/title_activity_home_spanish" >
</activity>
</application>
ERROR:
04-26 21:52:33.261: E/AndroidRuntime(6020): FATAL EXCEPTION: main
04-26 21:52:33.261: E/AndroidRuntime(6020): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edsonmarcelo.tapfun/com.example.edsonmarcelo.tapfun.Home SpanishActivity}: java.lang.NullPointerException
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.os.Looper.loop(Looper.java:176)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-26 21:52:33.261: E/AndroidRuntime(6020): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 21:52:33.261: E/AndroidRuntime(6020): at java.lang.reflect.Method.invoke(Method.java:525)
04-26 21:52:33.261: E/AndroidRuntime(6020): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-26 21:52:33.261: E/AndroidRuntime(6020): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-26 21:52:33.261: E/AndroidRuntime(6020): at dalvik.system.NativeStart.main(Native Method)
04-26 21:52:33.261: E/AndroidRuntime(6020): Caused by: java.lang.NullPointerException
04-26 21:52:33.261: E/AndroidRuntime(6020): at com.example.edsonmarcelo.tapfun.HomeSpanishActivity.onCreate(HomeSpanishActivity.java:21)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.Activity.performCreate(Activity.java:5372)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-26 21:52:33.261: E/AndroidRuntime(6020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
Englishomeactivity
package com.example.edsonmarcelo.tapfun;
public class HomeEnglishAcitivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_english_acitivity);
GridView gridview = (GridView) findViewById(R.id.gdViewHomeEnglish);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(HomeEnglishAcitivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_english_acitivity, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Spanishhomeactivity
package com.example.edsonmarcelo.tapfun;
public class HomeSpanishActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_spanish);
GridView gridview = (GridView) findViewById(R.id.gdViewHomeEnglish);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(HomeSpanishActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_spanish, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I made this change but the application opens and when I click on any of the buttons, it closes. Alguns erros abaixo: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.edsonmarcelo.tapfun/com.example.edsonmarcelo.tapfun.HomeSpanishActivity}: java.lang.NullPointerException
– Gabriel Rossetto
I added the bugs to the question
– Gabriel Rossetto
The error is then in the open Activity on line 21. Add its code as well.
– Paulo Rodrigues
Paul, I added in the question the code of the two Activities.
– Gabriel Rossetto
Solved! The problem was that I had a textview inside a gridview. Thanks anyway Paulo Rodrigues.
– Gabriel Rossetto