2015-06-12 13 views
5

पर वर्चुअल विधि 'void android.widget.TextView.setText (java.lang.CharSequence)' का आह्वान करने का प्रयास इरादे का उपयोग करके एक टुकड़े से दूसरे में डेटा पास करने में असमर्थ। तर्क ठीक है। कृपया मुख्य गतिविधि के अंतिम भाग देखें जहां मैंने putExtra का उपयोग करके अन्य गतिविधियों में तार भेज दिए हैं।एक शून्य ऑब्जेक्ट संदर्भ

Process: khurana.nikhil.lovemeter, PID: 4019 
java.lang.RuntimeException: Unable to start activity ComponentInfo{khurana.nikhil.lovemeter/khurana.nikhil.lovemeter.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
     at android.app.ActivityThread.access$800(ActivityThread.java:151) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5257) 
     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:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
     at khurana.nikhil.lovemeter.SecondActivity.onCreate(SecondActivity.java:32) 
     at android.app.Activity.performCreate(Activity.java:5990) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 

यहाँ:

public class SecondActivity extends Activity { 

    TextView tv7,tv8,tv9,tv10; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second); 


    Intent in = getIntent(); 

    String s1 = in.getStringExtra("k1"); 
    String s2 = in.getStringExtra("k2"); 
    String s3 = in.getStringExtra("k3"); 
    String s4 = in.getStringExtra("k4"); 
    tv7=(TextView)findViewById(R.id.textView3); 
    tv8=(TextView)findViewById(R.id.editText4); 
    tv9=(TextView)findViewById(R.id.editText5); 
    tv10=(TextView)findViewById(R.id.editText6); 

    tv7.setText("Hello "+s1); 
    tv8.setText("Your partner "+s2+" is really nice"); 
    tv9.setText("Your love percent based on name is "+s3); 
    tv10.setText("Your love percent based on age is "+s4); 
} 
} 

यहाँ त्रुटि मैं हो रही है:

public class MainActivity extends Activity implements View.OnClickListener { 
EditText ed1,ed2,ed3,ed4,ed5,ed6; 

Button bt1; 
TextView tv1,tv2; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ed1=(EditText)findViewById(R.id.editText); 
    ed2=(EditText)findViewById(R.id.editText2); 
    ed3=(EditText)findViewById(R.id.editText3); 
    ed4=(EditText)findViewById(R.id.editText4); 
    ed5=(EditText)findViewById(R.id.editText5); 
    ed6=(EditText)findViewById(R.id.editText6); 
    tv1=(TextView)findViewById(R.id.textView); 
    tv2=(TextView)findViewById(R.id.textView2); 
    bt1=(Button)findViewById(R.id.button); 
    bt1.setOnClickListener(this); 
} 
public void onClick(View v) 
{ 

    String str1=ed1.getText().toString(); 
    String str2=ed2.getText().toString(); 
    String str3=ed3.getText().toString(); 
    String str4=ed4.getText().toString(); 
    String str5=ed5.getText().toString(); 
    String str6=ed6.getText().toString(); 

    if(str1.matches("") || str2.matches("") || str3.matches("")) 
    { 
     tv1.setText("One or more fields missing"); 
     if(str4.matches("") || str5.matches("") || str6.matches("")) 
     { 
      tv2.setText("One or more fields missing"); 
     } 
    } 

    else { 
     int age1 = Integer.parseInt(ed2.getText().toString()); 
     int age2 = Integer.parseInt(ed5.getText().toString()); 

     int l1 = str1.length(); 
     str1 = str1.toLowerCase(); 
     str4 = str4.toLowerCase(); 
     int l2 = str4.length(); 

     int[] a = new int[26]; 
     int[] b = new int[26]; 

     for (int i = 0; i < l1; i++) { 

      char c = str1.charAt(i); 
      int x = (int) c - 97; 
      a[x]++; 
     } 
     for (int i = 0; i < l2; i++) { 

      char c = str4.charAt(i); 
      int x = (int) c - 97; 
      b[x]++; 
     } 
     int s=0,p; 
     for(int i=0;i<26;i++) 
     { 
      if(a[i]>b[i]) 
       p=a[i]; 
      else 
       p=b[i]; 

      s+=p; 
     } 
     if(l1>l2) 
      p=l1; 
     else 
      p=l2; 
     double love1=(double)(s/p)*100; 
     double love2=0.0,x; 

     if(age1>age2) { 
      p = age1; 
     x=age2; 
     } 
     else { 
      p = age2; 
      x=age1; 
     } 
     love2=(double)(x/p)*100; 

     Intent in=new Intent(MainActivity.this,SecondActivity.class); 
     String s1=""+love1; 
     String s2=""+love2; 



     in.putExtra("k1",str4); 
     in.putExtra("k2",str4); 
     in.putExtra("k3", love1); 
     in.putExtra("k4",love2); 
     startActivity(in); 
     finish(); 


    } 


} 


} 

यहाँ SecondActivity.java के लिए कोड है:

यहाँ MainActivity.java है गतिविधि_second.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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:background="#cefcfd" 
tools:context="khurana.nikhil.lovemeter.SecondActivity"> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:id="@+id/textView3" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="68dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:id="@+id/textView4" 
    android:layout_below="@+id/textView3" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="51dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:id="@+id/textView5" 
    android:layout_above="@+id/textView6" 
    android:layout_alignStart="@+id/textView6" 
    android:layout_marginBottom="84dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:id="@+id/textView6" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="51dp" /> 
</RelativeLayout> 

+2

गतिविधि_second.xml कृपया जोड़ें। – mrtn

+0

निम्नलिखित लिंक का संदर्भ लें। https://vshivam.wordpress.com/2015/01/14/inflating-different-xml-layouts-in-an-android-listview-for-different-objects/ –

+0

संभावित डुप्लिकेट [NullPointerException क्या है, और कैसे क्या मैं इसे ठीक करता हूं?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Denny

उत्तर

7
tv8=(TextView)findViewById(R.id.editText4); 
tv9=(TextView)findViewById(R.id.editText5); 
tv10=(TextView)findViewById(R.id.editText6); 

बदलें इन करने के लिए:

tv8=(TextView)findViewById(R.id.textView4); 
tv9=(TextView)findViewById(R.id.textView5); 
tv10=(TextView)findViewById(R.id.textView6); 

तुम बस गलत आईडी का इस्तेमाल किया।

+0

xml फ़ाइल पर एक नज़र डालें। आईडी कोड से अलग हैं। यदि आईडी अलग-अलग विचारों के लिए उपयोग किया जाता है तो यह 'क्लासकास्ट अपवाद' फेंक देगा। लेकिन चूंकि वे मौजूद नहीं हैं, इसलिए यह वहां 'शून्य' देता है। – mrtn

+0

धन्यवाद दोस्त। यह काम किया..क्या एक मूर्खतापूर्ण गलती थी .. एक और समस्या: ऐप एमुलेटर में ठीक काम करता है लेकिन मोबाइल पर काम नहीं करता है। यह एक त्रुटि उत्पन्न करता है: पैकेज को पार्स करने में समस्या। –

+0

@mrtn। क्या आप कृपया मेरे प्रश्न का उत्तर देंगे? http://stackoverflow.com/q/40817284/4568864 –

संबंधित मुद्दे