私はアンドロイド開発の初心者です。そして、私のコードに問題があります。アプリケーションが完全にクラッシュしました。メインのJavaエラーです。
MainActivity.java:
package hu.cgkni6.uniobuda.testapplication;
import hu.cgkni6.uniobuda.testapplication.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
final String username = "diak";
final String pass = "diak";
Button okbutton = (Button) findViewById(R.id.btn_OK);
Button cancelbutton = (Button) findViewById(R.id.btn_Cancel);
final EditText usrname = (EditText) findViewById(R.id.editText1);
final EditText passwd = (EditText) findViewById(R.id.editText2);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
okbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (usrname.getText().toString().equals(username))
{
if (passwd.getText().toString().equals(pass))
{
String text2 = "Success!";
Toast.makeText(MainActivity.this, text2, Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Invalid Password", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(MainActivity.this, "Invalid username", Toast.LENGTH_LONG).show();
}
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
@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;
}
}
Activity_main.xml:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#45B7FA" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:text="@string/login_interface" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/username" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="@string/password"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/password" >
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_OK"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="@string/ok" />
<Button
android:id="@+id/btn_Cancel"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="45dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:textSize="20sp"
android:text="@string/cancel" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="@string/about_us"
/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="@string/contact_us" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:background="#FFFFFF"
android:textColor="#000000"
android:text="@string/help" />
</TableRow>
</RelativeLayout>
</LinearLayout>
カタログ:
10-09 09:18:26.244: E/AndroidRuntime(302): FATAL EXCEPTION: main
10-09 09:18:26.244: E/AndroidRuntime(302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hu.cgkni6.uniobuda.testapplication/hu.cgkni6.uniobuda.testapplication.MainActivity}: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.os.Looper.loop(Looper.java:123)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invokeNative(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.reflect.Method.invoke(Method.java:521)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-09 09:18:26.244: E/AndroidRuntime(302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-09 09:18:26.244: E/AndroidRuntime(302): at dalvik.system.NativeStart.main(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): Caused by: java.lang.NullPointerException
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Activity.findViewById(Activity.java:1637)
10-09 09:18:26.244: E/AndroidRuntime(302): at hu.cgkni6.uniobuda.testapplication.MainActivity.<init>(MainActivity.java:17)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstanceImpl(Native Method)
10-09 09:18:26.244: E/AndroidRuntime(302): at java.lang.Class.newInstance(Class.java:1429)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-09 09:18:26.244: E/AndroidRuntime(302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
10-09 09:18:26.244: E/AndroidRuntime(302): ... 11 more