我希望找到这个问题的答案。我试图在图像按钮中插入onclick监听器以打开一个新活动,每当我将其运行到模拟器并单击图像按钮时,此消息显示“不幸的是我的应用程序已停止。”
到目前为止我只完成了2项活动,我想要发生的是当我点击图像按钮时它会转到我的活动2。
他是我的java代码的一个例子。
package com.example.chadymaebarinan.emojiexpress;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
public class HomeScreen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
ImageButton clickGirl = (ImageButton)findViewById(R.id.myGirl);
clickGirl.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(HomeScreen.this, Emoticon.class));
}
});
}
@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_screen, 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);
}
}
这是emoticon.java中的代码
package com.example.chadymaebarinan.emojiexpress;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Emoticon extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoticon);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
并在清单文件中:
?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".HomeScreen"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Emoticon"
android:label="@string/title_activity_emoticon"
android:theme="@style/AppTheme.NoActionBar" >
</activity>
</application>
我尝试点击此处的图片按钮时的Logcat:
10-24 19:47:09.202 3777-3777/? I/art: Not late-enabling -Xcheck:jni (already on)
10-24 19:47:09.202 3777-3777/? I/art: Late-enabling JIT
10-24 19:47:09.245 3777-3777/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
10-24 19:47:09.410 3777-3777/com.example.chadymaebarinan.emojiexpress W/System: ClassLoader referenced unknown path: /data/app/com.example.chadymaebarinan.emojiexpress-1/lib/x86
10-24 19:47:10.319 3777-3804/com.example.chadymaebarinan.emojiexpress D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
10-24 19:47:10.347 3777-3777/com.example.chadymaebarinan.emojiexpress D/: HostConnection::get() New Host Connection established 0xabeeaf20, tid 3777
10-24 19:47:10.540 3777-3804/com.example.chadymaebarinan.emojiexpress I/OpenGLRenderer: Initialized EGL, version 1.4
10-24 19:47:10.598 3777-3804/com.example.chadymaebarinan.emojiexpress W/EGL_emulation: eglSurfaceAttrib not implemented
10-24 19:47:10.600 3777-3804/com.example.chadymaebarinan.emojiexpress W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabf30ae0, error=EGL_SUCCESS
10-24 19:47:13.410 3777-3777/com.example.chadymaebarinan.emojiexpress D/AndroidRuntime: Shutting down VM
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: FATAL EXCEPTION: main
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: Process: com.example.chadymaebarinan.emojiexpress, PID: 3777
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chadymaebarinan.emojiexpress/com.example.chadymaebarinan.emojiexpress.Emoticon}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at com.example.chadymaebarinan.emojiexpress.Emoticon.onCreate(Emoticon.java:17)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-24 19:49:52.102 3777-3784/com.example.chadymaebarinan.emojiexpress W/art: Suspending all threads took: 36.756ms
答案 0 :(得分:0)
试试这段代码
public class HomeScreen extends AppCompatActivity implements View.OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
ImageButton clickGirl = (ImageButton) findViewById(R.id.myGirl);
clickGirl
}
public void onClick(View v) {
switch (v.getId())
{
case R.id.fab:
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
break;
case R.id.myGirl:
Intent intent = new Intent(HomeScreen.this,Emoticon.class);
startActivity(intent);
finish();
}
}
并添加Emoticon class
此
Intent intent = getIntent();
答案 1 :(得分:0)
最后在寻找解决方案2天后找到了答案。在阅读logcat之后,我发现由于我的nullpointer异常导致我的图像按钮失效的主要错误:
10-24 19:47:13.411 3777-3777/com.example.chadymaebarinan.emojiexpress E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
并修复了我的emoticon.class,这是一个更新版本:
package com.example.chadymaebarinan.emojiexpress;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Emoticon extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoticon);
Intent intent = getIntent();
}
}
删除了导致nullpointer的工具栏和Snackbar。希望这对与我有同样问题的人有所帮助。 :)