自从我决定自己构建一个Android应用程序以来,已经过了几天,似乎我需要一些帮助。我有一般的java和编程经验,虽然我在Android应用程序开发中完全是noob。在完成了几个教程和视频之后,我最终在我的Linux薄荷操作系统上使用了eclipse。 java版本的结果检查:
java -version
java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
至于编译器:
javac -version
javac 1.8.0_66
第一个样本"你好世界"应用程序似乎在虚拟设备和我的LG上运行良好。因此,最初的试验之一是创建一个带有2个文本字段和2个按钮的简单应用程序,点击它会导致在不同的字段中打印嘿。我的核心文件是:
java文件
package com.alexkarvou.proj_1;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
public class MainActivity extends Activity {
EditText input;
EditText output;
Button one;
ImageButton two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input =(EditText)findViewById(R.id.editText1);
output =(EditText)findViewById(R.id.editText2);
one=(Button)findViewById(R.id.button1);
two=(ImageButton)findViewById(R.id.imageButton1);
one.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(v==one){
Context context =getApplicationContext();
CharSequence text ="Hey dude";
int duration =Toast.LENGTH_LONG;
final Toast toast =Toast.makeText(context,text,duration);
toast.show();
}
}
});
two.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(v==two){
output.setText("Hey dude");
}
}
});
}
@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;
}
@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);
}
/**
* 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;
}
}
}
fragment_main XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.alexkarvou.proj_1.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="@string/Title_ref"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="60sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginLeft="35dp"
android:layout_marginTop="32dp"
android:ems="10" android:inputType="text">
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" android:inputType="text"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText2"
android:layout_marginLeft="30dp"
android:layout_marginTop="53dp"
android:text="@string/button_ref" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_alignTop="@+id/button1"
android:layout_marginRight="19dp"
android:src="@drawable/ic_launcher" android:contentDescription="@string/image_ref"/>
</RelativeLayout>
activity_main XML文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.alexkarvou.proj_1.MainActivity"
tools:ignore="MergeRootFrame" />
logcat文件
11-15 16:46:48.936: I/art(1385): Not late-enabling -Xcheck:jni (already on)
11-15 16:46:48.938: I/art(1385): Late-enabling JIT
11-15 16:46:49.049: I/art(1385): JIT created with code_cache_capacity=2MB compile_threshold=1000
11-15 16:46:49.785: W/System(1385): ClassLoader referenced unknown path: /data/app/com.alexkarvou.proj_1-2/lib/arm
11-15 16:46:50.570: D/AndroidRuntime(1385): Shutting down VM
11-15 16:46:50.570: D/AndroidRuntime(1385): --------- beginning of crash
11-15 16:46:50.575: E/AndroidRuntime(1385): FATAL EXCEPTION: main
11-15 16:46:50.575: E/AndroidRuntime(1385): Process: com.alexkarvou.proj_1, PID: 1385
11-15 16:46:50.575: E/AndroidRuntime(1385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexkarvou.proj_1/com.alexkarvou.proj_1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.os.Handler.dispatchMessage(Handler.java:102)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.os.Looper.loop(Looper.java:148)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.ActivityThread.main(ActivityThread.java:5417)
11-15 16:46:50.575: E/AndroidRuntime(1385): at java.lang.reflect.Method.invoke(Native Method)
11-15 16:46:50.575: E/AndroidRuntime(1385): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-15 16:46:50.575: E/AndroidRuntime(1385): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-15 16:46:50.575: E/AndroidRuntime(1385): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
11-15 16:46:50.575: E/AndroidRuntime(1385): at com.alexkarvou.proj_1.MainActivity.onCreate(MainActivity.java:35)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.Activity.performCreate(Activity.java:6237)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-15 16:46:50.575: E/AndroidRuntime(1385): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-15 16:46:50.575: E/AndroidRuntime(1385): ... 9 more
我希望得到一些帮助,或者至少有一些重定向,我应该在哪里运行我的错误并掌握整个过程。 提前谢谢。
答案 0 :(得分:0)
你有java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
在创建活动之前,按钮中的View v
为空..首先尝试不使用if(v==one){
:
one.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Click one!!", Toast.LENGTH_SHORT).show();
}
});
two.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Click two", Toast.LENGTH_SHORT).show();
}
});
然后确保有一个选项,其中视图为null或尝试不同的
答案 1 :(得分:0)
更改
input =(EditText)findViewById(R.id.editText1);
output =(EditText)findViewById(R.id.editText2);
one=(Button)findViewById(R.id.button1);
two=(ImageButton)findViewById(R.id.imageButton1);
到
View view = getLayoutInflater().inflate(R.layout.fragment_main, null);
input =(EditText)view.findViewById(R.id.editText1);
output =(EditText)view.findViewById(R.id.editText2);
one=(Button)view.findViewById(R.id.button1);
two=(ImageButton)view.findViewById(R.id.imageButton1);
原因是您试图在EditText
布局中找到属于Button
的{{1}},ImageButton
和activity_main.xml
组件。< / p>
同时建议 - Google不再支持Eclipse,您应该将Android Studio用于下一个项目。