我正在尝试根据我已经拥有的网站功能制作一个具有多项功能的应用。基本上我正在创建一个伴侣应用程序。话虽这么说,当我按下主页上的按钮启动该功能时,应用程序崩溃。我已经搜索了互联网,尝试了我能找到的每一个解决方案,但到目前为止还没有工作。奇怪的是,除了在创建类时生成的代码之外没有其他代码,它才能工作。
这是我的主要活动:
package com.example.nicole.signal;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import com.example.nicole.signal.R;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button Calls
Button forumbtn = (Button) findViewById(R.id.forum_btn);
forumbtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
startActivity(new Intent(MainActivity.this, ForumActivity.class));
}
});
Button webbtn = (Button) findViewById(R.id.Web_btn);
webbtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
startActivity(new Intent(MainActivity.this, WebActivity.class));
}
});
Button chatbtn = (Button) findViewById(R.id.Chat_btn);
chatbtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
startActivity(new Intent(MainActivity.this, ChatActivity.class));
}
});
Button videobtn = (Button) findViewById(R.id.Video_btn);
videobtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
startActivity(new Intent(MainActivity.this, VideoActivity.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_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);
}
}
我正在调用崩溃的一个类:
package com.example.nicole.signal;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class WebActivity extends MainActivity
{
private final WebView mWebView = (WebView) findViewById(R.id.activity_web);
@Override
protected void onCreate(Bundle savedInstanceState) {
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
Uri uri = Uri.parse("http://hnsignal.ca");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
}
}
这是logcat错误:
02-26 17:33:37.895 29086-29086/? E/Zygote: MountEmulatedStorage()
02-26 17:33:37.895 29086-29086/? E/Zygote: v2
02-26 17:33:37.915 29086-29086/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-26 17:33:48.976 29086-29086/com.example.nicole.signal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nicole.signal, PID: 29086
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nicole.signal/com.example.nicole.signal.WebActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:151)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.nicole.signal.WebActivity.<init>(WebActivity.java:14)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
非常感谢任何帮助。在构建期间没有错误被抛出,所以我想它在我的代码中的某个地方...我只是无法找到它。谢谢
答案 0 :(得分:2)
您的代码存在一些问题。首先
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
这些必须是onCreate()
的前两行。 setContentView()
创建视图对象,因此在调用此方法之前,您无法访问任何视图对象。这也意味着您无法像在线一样初始化字段变量:
private final WebView mWebView = (WebView) findViewById(R.id.activity_web);
相反,您必须在onCreate()
内执行此操作。您可以在MainActivity
中正确执行此操作。以此为指导。
您还应将WebActivity
更改为
public class WebActivity extends AppCompatActivity
你不应该延长MainActivity
。
答案 1 :(得分:0)
private final WebView mWebView;
此代码是问题,您应该将webview声明为
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.activity_web);
WebSettings webSettings = mWebView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setJavaScriptEnabled(true);
Uri uri = Uri.parse("http://hnsignal.ca");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
并在setContentView之后设置其值
"gddgdg": "John", "firstName": "545", "dgdfg": "John",
"lastName": "Smith", "sgsddg": "John", "firstName": "666",
"firstName": "333", "sdfdf": "John", "sffdsf": "John",
因为setContentView会使布局膨胀,并且在布局膨胀之前无法使用findViewById找到视图。并且您设置webview的所有代码都应该低于查找webview的代码