显示java.lang.NullPointerException

时间:2013-11-26 14:55:48

标签: java android mysql listview nullpointerexception

我开发了一个简单的listview应用程序,它从mysql数据库中获取数据。它显示了listview,但是当我选择单个列表视图时,它会出现以下错误。

这是我单个列表视图的xml文件,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<TextView android:id="@+id/product_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dip"
        android:textStyle="bold"
        android:padding="10dip"
        android:textColor="#ffffff"/>   
</LinearLayout>

MainActivity.java文件,

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity implements FetchDataListener{
private ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.activity_main);        
    initView(); 

    ListView lv = getListView();

    // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {

          // selected item
          TextView txtview = ((TextView) view.findViewById(R.id.product_label));
          String product = txtview.getText().toString();

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          startActivity(i);

      }
    });

}

private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");

    String url = "http://pubbapp.comze.com/pubapp.php";
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}

@Override
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);        
}

@Override
public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
}
}

这是单个列表活动java文件,

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list_item_view);

    TextView txtProduct = (TextView) findViewById(R.id.product_label);

    Intent i = getIntent();
    // getting attached intent data
    String product = i.getStringExtra("product");
    // displaying selected product name
    txtProduct.setText(product);

}
}

这是错误消息,

11-27 00:11:25.210: E/AndroidRuntime(9924): FATAL EXCEPTION: main
11-27 00:11:25.210: E/AndroidRuntime(9924): java.lang.NullPointerException
11-27 00:11:25.210: E/AndroidRuntime(9924):     at com.sj.jsondemo.MainActivity$1.onItemClick(MainActivity.java:35)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.widget.ListView.performItemClick(ListView.java:3745)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.os.Handler.handleCallback(Handler.java:587)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.os.Looper.loop(Looper.java:130)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at android.app.ActivityThread.main(ActivityThread.java:3691)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at java.lang.reflect.Method.invokeNative(Native Method)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at java.lang.reflect.Method.invoke(Method.java:507)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
11-27 00:11:25.210: E/AndroidRuntime(9924):     at dalvik.system.NativeStart.main(Native Method)
11-27 00:11:59.925: I/Process(9924): Sending signal. PID: 9924 SIG: 9

我试图解决这个问题,但我找不到任何解决方案。请帮助我。

2 个答案:

答案 0 :(得分:0)

请完整支持activity_main.xml
在您的activity_main.xml中应该包含ListView,如下所示:

ListView android:id="@id/android:list"

答案 1 :(得分:0)

这是因为,当您单击该项时,它指向空值,可能没有可从数据库中获取的值。请在该空指针位置保留一个断点,并查看其检索的值。