[英]Unable to start activity ComponentInfo NullpointerException --
[英]Unable to start activity componentinfo Nullpointerexception
我對Java完全陌生,我正在嘗試創建一個可向我的PC上運行的Visual Basic程序發送UDP(Google的UDP部件代碼)的應用。 我還沒有使用文本框,但是當我按下按鈕時,它僅發送文本字符串(至少我希望它這樣做,但是由於錯誤,我什至沒有到達那部分。我已經搜索了幾乎整個google xD,但是我找不到任何對我有幫助的東西(至少我的理解)我知道這是一個已經問了很多次的問題了,但是我不能從他們身上得到更明智的選擇:P
Kere是logcat列表:
08-10 22:02:22.519: E/AndroidRuntime(16182): FATAL EXCEPTION: main
08-10 22:02:22.519: E/AndroidRuntime(16182): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.keetdoaldiek/com.example.keetdoaldiek.Keet}: java.lang.NullPointerException
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.os.Looper.loop(Looper.java:137)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.ActivityThread.main(ActivityThread.java:5260)
08-10 22:02:22.519: E/AndroidRuntime(16182): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 22:02:22.519: E/AndroidRuntime(16182): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 22:02:22.519: E/AndroidRuntime(16182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-10 22:02:22.519: E/AndroidRuntime(16182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-10 22:02:22.519: E/AndroidRuntime(16182): at dalvik.system.NativeStart.main(Native Method)
08-10 22:02:22.519: E/AndroidRuntime(16182): Caused by: java.lang.NullPointerException
08-10 22:02:22.519: E/AndroidRuntime(16182): at com.example.keetdoaldiek.Keet.senbutton(Keet.java:39)
08-10 22:02:22.519: E/AndroidRuntime(16182): at com.example.keetdoaldiek.Keet.onCreate(Keet.java:28)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.Activity.performCreate(Activity.java:5104)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-10 22:02:22.519: E/AndroidRuntime(16182): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-10 22:02:22.519: E/AndroidRuntime(16182): ... 11 more
這是我的main.java
package com.example.keetdoaldiek;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Keet extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_keet);
sendbutton();
}
private void sendbutton() {
Button messagebutton = (Button)findViewById(android.R.id.button1);
messagebutton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
String messageStr = "Hello Android!";
int server_port = 54372;
try
{
DatagramSocket s = new DatagramSocket();
InetAddress local = null;
local = InetAddress.getLocalHost();
int msg_length = messageStr.length();
byte[] message = messageStr.getBytes();
DatagramPacket p = new DatagramPacket(message, msg_length, local, server_port);
s.send(p);
s.close();
android.util.Log.w("UDP", "Works fine!");
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
android.util.Log.w("UDP", "Catched here.");
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.keet, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這是我的清單。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.keetdoaldiek"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="Keet"
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>
我認為它的變量是什么? 但我不知道哪一個。 我希望你們能幫助我解決我的問題。
謝謝,
您沒有在清單中正確聲明的Keet活動。 您可以使用com.example.keetdoaldiek.Keet
或.Keet
作為android:name
,如下所示:
<activity
android:name="com.example.keetdoaldiek.Keet"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
根據官方的android文檔 :
android:name實現活動的類的名稱,是Activity的子類。 該屬性值應為完全限定的類名稱 (例如“ com.example.project.ExtracurricularActivity”)。 但是,作為簡寫形式,如果名稱的第一個字符是句點 (例如,“。ExtracurricularActivity”), 則將其追加到element中指定的包名稱后面 。 發布應用程序后,請勿更改此名稱(除非您已設置android:exported =“ false”)。
沒有默認值。 必須指定名稱。
在您的函數sendbutton()
更改
Button messagebutton = (Button)findViewById(android.R.id.button1);
至
Button messagebutton = (Button)findViewById(R.id.button1);
和使用
com.example.keetdoaldiek.R
這是因為android.R
適用於一般的Android資源,而com.example.keetdoaldiek.R
是為您的應用生成的索引。
更改
Button messagebutton = (Button)findViewById(android.R.id.button1);
至
Button messagebutton = (Button)findViewById(R.id.button1);
第二個問題是您的清單缺少使用Internet的聲明,在這種情況下,這是必需的:將以下內容放在清單中application標簽之外。
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.